package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC481 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int magicalString(int n) {
            if (n <= 0) return 0;
            if (n <= 3) return 1;
            int[] seq = new int[n + 1];
            seq[1] = 1;
            seq[2] = 2;
            seq[3] = 2;

            int moveIdx = 3, curRepeatCount = 0, oneCount = 1;
            boolean isCurOne = true;
            for (int i = 4; i <= n; i++) {
                if (isCurOne) {
                    seq[i] = 1;
                    ++oneCount;
                } else {
                    seq[i] = 2;
                }

                if (++curRepeatCount >= seq[moveIdx]) {
                    ++moveIdx;
                    curRepeatCount = 0;
                    isCurOne = !isCurOne;
                }
            }

            return oneCount;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.magicalString(10));
    }

    public static void main(String[] args) throws Exception {
        LC481 an = new LC481();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
